package AddPresent;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import AddProduct.HTML;
import AddProduct.SQL;
import AddProduct.SQLStatements;

/**
 * Servlet implementation class ChooseItem
 * This servlet insert data into PRESENTS table in database and gives option to customer to choose for what item is this present.
 * @author desso
 */
@WebServlet("/ChooseItem")
public class ChooseItem extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChooseItem() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 try {			  
			 	request.setCharacterEncoding("UTF-8");
			 	Connection databaseConnection = SQL.getConnection();
				insertIntoPresents(request, databaseConnection);
				System.out.println(SQLStatements.STATMENT_FOR_ITEMS);
				ResultSet  resultSetForItems = SQL.getDataFromDatabase(databaseConnection, SQLStatements.STATMENT_FOR_ITEMS);
				PrintWriter prinWriter = response.getWriter();
				response.setContentType("text/html");
				
				prinWriter.println("<!DOCTYPE html>");
				prinWriter.println("<html>");
				prinWriter.println("   <head>");
				prinWriter.println("   </head>");
				prinWriter.println("    <body>");
				prinWriter.println("      <h3> Please choose for what item is this present! </h3>");		
				prinWriter.println("      <form name=\"new present\" action=\"InsertIntoItemPresent\" method=\"post\">");			
				HTML.printItems(prinWriter, resultSetForItems);			
				prinWriter.println("         <input type=\"submit\" value=\"Continue\">");	
				prinWriter.println("      </form> ");
				prinWriter.println("    </body>");
				prinWriter.println("</html>");

			} catch (SQLException | ClassNotFoundException e) {
				e.printStackTrace();
			}		
		}
		
		private void insertIntoPresents(HttpServletRequest request, Connection dbConnection) throws SQLException {	
			HttpSession sess = request.getSession(true);
			PreparedStatement prepareStatement = dbConnection.prepareStatement(SQLStatements.INSERT_INTO_PRESENTS_TABLE, new String[] { "id" });
			prepareStatement.setString(1, request.getParameter("description"));
			prepareStatement.setString(2, request.getParameter("figure_path"));
			prepareStatement.setInt(3, Integer.parseInt((String) request.getParameter("count")));
			prepareStatement.executeUpdate();	
			
			ResultSet rs = prepareStatement.getGeneratedKeys();
			if (rs != null && rs.next()) {
			    sess.setAttribute("present_id", rs.getLong(1));
			}
		}
}
